<?php
namespace Api\Controller;

/**
 * 附近人
 */
class NearbyPeopleController extends ApiController
{
    protected $noCheck = [];//['info','videos'];

    function _initialize() {
        parent::_initialize();
    }

    /**
     * 设置自己附近人信息
     */
    public function edit()
    {
        $imgs = I('imgs');
        $video = I('video');
        $sign = I('sign');
        $height = I('height');
        $weight = I('weight');
        $hobby = I('hobby');
        $age = I('age');
        $pro = I('pro');
        $city = I('city');
        $area = I('area');
        $address = I('address');
        $lat = I('lat');
        $lng = I('lng');
        $constellation = I('constellation');
        $skill = I('skill');
        $img = '';
        $res = file_get_contents("http://api.map.baidu.com/geocoding/v3/?address={$pro}{$city}{$area}{$address}&output=json&ak=GtHo4PQviIuEZWK5ycAyBD8CjE9MHnRG");
        if($res && $res = json_decode($res, true)){
            if($res['status'] === 0){
                $lat = $res['result']['location']['lat'];
                $lat = $res['result']['location']['lat'];
            }
        }
        if(empty($sign)){
            $this->ajaxReturn(['s' => false, 'm' => '请输入个性签名']);
        }
        if(empty($height)){
            $this->ajaxReturn(['s' => false, 'm' => '请输入身高']);
        }
        if(empty($weight)){
            $this->ajaxReturn(['s' => false, 'm' => '请输入体重']);
        }
        if(empty($age)){
            $this->ajaxReturn(['s' => false, 'm' => '请输入年龄']);   
        }
        if(empty($city)){
            $this->ajaxReturn(['s' => false, 'm' => '请输入城市']);
        }
        if(empty($constellation)){
            $this->ajaxReturn(['s' => false, 'm' => '请输入星座']);
        }
        if(is_array($imgs)){
            if($imgs){
                $img = $imgs[0];
            }
            $imgs = implode(',', $imgs);
        }else{
            if(!empty($imgs)){
                $imgArr = explode(',', $imgs);
                if(!empty($imgArr[0])){
                    $img = $imgArr[0];
                }
            }
        }
        if(empty($imgs)){
            $this->ajaxReturn(['s' => false, 'm' => '请上传首页轮播图']);
        }
        $data = [
            'nearby_img'    => $img,
            'nearby_imgs'   => $imgs,
            'nearby_video'  => $video,
            'sign_name'     => $sign,
            'height'        => $height,
            'weight'        => $weight,
            'hobby'         => $hobby,
            'nearby_age'    => $age,
            'nearby_pro'    => $pro,
            'nearby_city'   => $city,
            'nearby_area'   => $area,
            'nearby_address'=> $address,
            'nearby_lat'    => $lat,
            'nearby_lng'    => $lng,
            'constellation' => $constellation,
            'skill'         => $skill,
            'is_nearby'     => 1,
        ];
        $res = M('user')->where(['id'=>$this->uid])->save($data);
        if($res === false){
            $this->ajaxReturn(['s'=>false, 'm'=>'提交失败']);
        }
        $this->ajaxReturn(['s'=>true, 'm'=>'提交成功']);
    }

    public function user()
    {
        $imgs = $this->user['nearby_imgs'] ? explode(',', $this->user['nearby_imgs']) : [];
        $imgs = array_filter($imgs);
        $user = [
            'imgs'          => $imgs,
            'video'         => $this->user['nearby_video'],
            'sign'          => $this->user['sign_name'],
            'height'        => $this->user['height'],
            'weight'        => $this->user['weight'],
            'hobby'         => $this->user['hobby'],
            'age'           => $this->user['nearby_age'],
            'pro'           => $this->user['nearby_pro'],
            'city'          => $this->user['nearby_city'],
            'area'          => $this->user['nearby_area'],
            'address'       => $this->user['nearby_address'],
            'constellation' => $this->user['constellation'],
            'skill'         => $this->user['skill'],
        ];
        $this->ajaxReturn(['s'=>true, 'm'=>'ok', 'info'=>$user]);
    }

    /**
     * 列表
     */
    public function search()
    {
        //经度
        $lng = I('lng');
        //纬度
        $lat = I('lat');
        $where = [];
        $where['is_nearby'] = 1;
        // I('pro') && $where['nearby_pro'] = I('pro');
        // I('city') && $where['nearby_city'] = I('city');
        // I('area') && $where['nearby_area'] = I('area');
        $list = M('user')->where($where)->field('id,nickname,img as avatar,nearby_img as img,nearby_age as age,constellation,follow_num,sign_name,nearby_lat as lat,nearby_lng as lng')->select();
        if($list){

            $amap_key = 'd7cdde040024dc1fe22157970ba6bd6b';
            $origins = '';
            foreach ($list as $key => $row) {
                $origins.= ($origins ? "|":'').$row['lng'].','.$row['lat'];
            }
            $url = "http://restapi.amap.com/v3/assistant/coordinate/convert?key={$amap_key}&locations={$origins}&coordsys=baidu";
            $res = curl_get($url);
            $res = json_decode($res, true);
            $origins = '';
            if($res['status'] == '1'){
                $locations = $res['locations'];
                foreach (explode(';', $locations) as $key => $row) {
                    $origins.= ($origins ? "|":'').explode(',', $row)[0].','.explode(',', $row)[1];
                }
            }
            $url = "http://restapi.amap.com/v3/assistant/coordinate/convert?key={$amap_key}&locations={$lng},{$lat}&coordsys=gps";
            $res = curl_get($url);
            $res = json_decode($res, true);
            if($res['status'] == '1'){
                $lng = explode(',', $res['locations'])[0];
                $lat = explode(',', $res['locations'])[1];
            }
            try{
                require_once 'ThinkPHP/Library/Vendor/autoload.php';
                $res = \yishuixm\lbs\LbsAmap::distance($amap_key, $origins, $lng.','.$lat, 1);
                $res = json_decode($res, true);
                if($res['status'] == 1){
                    foreach ($list as $key => &$row) {
                        $row['distance'] = $res['results'][$key]['distance'];
                    }
                    array_multisort(array_column($list, 'distance'), SORT_ASC, $list);
                }
            }catch(\Exception $e){

            }

        }
        // foreach ($list as $key => &$row) {
        //     $distance = ($row['lng'] - $lng) * ($row['lng'] - $lng) + ($row['lat'] - $lat) * ($row['lat'] - $lat);
        //     $row['distance'] = round(sqrt($distance), 5);
        // }
        // $list = $this->array_sort($list, 'distance', 'asc');
        // $list = array_values($list);
        $this->ajaxReturn(array('s' => true, 'd' => $list));
    }

    /**
     * 自己是否是附近人
     */
    public function is_nearby_user()
    {
        $this->ajaxReturn(array('s' => true, 'd' => $list, 'is_nearby'=>$this->user['is_nearby']));
    }

    public function dynamic_add()
    {
        $post = I('post.');
        $img_is_base64 = I('img_is_base64', 1);
        if(empty($post['desc']) && empty($post['imgs']) && empty($post['video'])){
            $this->ajaxReturn(['s' => false, 'm' => '请上传内容']);

        }
        if(isset($post['imgs']) && $post['imgs']){
            if($img_is_base64){
                $post['imgs'] = $this->uploadimgsy('imgs');
            }
        }

        $post['user_id'] = $this->uid;
        $post['status'] = 1;
        $post['create_time'] = time();
        $post['update_time'] = time();
        $res = M('user_dynamic')->add($post);
        if(!$res){
            $this->ajaxReturn(['s'=>false, 'm'=>'提交失败']);
        }
        $this->ajaxReturn(['s'=>true, 'm'=>'提交成功']);
    }

    public function dynamic_list()
    {
        $user_id = I('user_id');
        if(empty($user_id)){
            $this->ajaxReturn(['s'=>false, 'm'=>'参数错误']);
        }
        $user = M('user')->where(['id'=>$user_id])->field('nickname,img')->find();
        $list = M('user_dynamic')->where(['user_id'=>$user_id, 'status'=>1])->order('id desc')->select();
        if($list){
            $siteurl = C("SiteUrl");
            require_once 'ThinkPHP/Library/Vendor/autoload.php';
            $ffmpeg = new \Common\Util\FFMpeg();
            $ffmpeg = $ffmpeg->ffmpeg;
            $cover_path = './'.C('UPLOAD_PATH').'/videocover/';
            if(!is_dir($cover_path)){
                mkdir($cover_path, 0755, true);
            }
            foreach ($list as $key => &$row) {
                $row['imgs'] = $row['imgs'] ? explode(',', $row['imgs']) : [];
                $video_cover = '';
                if($row['video']){
                    try {
                        // 视频路径
                        $video_path = str_replace($siteurl, './', $row['video']);
                        if(!is_file($video_path)){
                            $video_path = str_replace('http://www.yuzhuli.com/', './', $row['video']);
                        }
                        if(is_file($video_path)){
                            $poster_path = $cover_path.md5($video_path).'.jpg';// 生成封面保存路径
                            if(!is_file($poster_path)){
                                $video = $ffmpeg->open($video_path);
                                $frame = $video->frame(\FFMpeg\Coordinate\TimeCode::fromSeconds(1));
                                $frame->save($poster_path, false);  
                            }
                            $video_cover = str_replace('/./', '/', $siteurl.$poster_path);
                        }
                    } catch (\Exception $e) {
                        trace('生成图片失败:'.$e->getMessage(), 'error');
                    }
                }
                $row['video_cover'] = $video_cover;
            }
        }
        if($this->uid == $user_id){
            $is_self = 1;
        }else{
            $is_self = 0;
        }
        $this->ajaxReturn(['s' => true, 'd' => $list, 'is_self'=>$is_self, 'user'=>$user]);
    }

    public function dynamic_del()
    {
        $dynamic_id = I('dynamic_id');
        if(empty($dynamic_id)){
            $this->ajaxReturn(['s'=>false, 'm'=>'参数错误']);
        }
        $info = M('user_dynamic')->where(['id'=>$dynamic_id])->find();
        if(empty($info)){
            $this->ajaxReturn(['s'=>false, 'm'=>'不存在或已删除']);
        }
        if($info['user_id']!=$this->uid){
            $this->ajaxReturn(['s'=>false, 'm'=>'非法删除操作']);
        }
        M('user_dynamic')->where(['id'=>$dynamic_id])->delete();
        $this->ajaxReturn(['s'=>true, 'm'=>'删除成功']);
    }

    /**
     * 详情
     */
    public function info()
    {
        //经度
        $lng = I('lng');
        //纬度
        $lat = I('lat');

        $id = I('id');
        // 信息
        $info = M('user')->where(['id'=>$id, 'is_nearby'=>1])->field('id,nickname,phone,img as avatar,nearby_imgs,nearby_video,sign_name,height,weight,hobby,nearby_city,constellation,follow_num,skill,nearby_lat,nearby_lng')->find();
        if(empty($info)){
            $this->ajaxReturn(array('s' => false, 'm' => "信息不存在"));
        }
        $lat = $info['nearby_lat'];
        $lng = $info['nearby_lng'];
        $info['nearby_imgs'] = $info['nearby_imgs'] ? explode(',', $info['nearby_imgs']) : [];
        // 动态
        $dynamics = M('user_dynamic')->where(['user_id'=>$info['id'], 'status'=>1])->order('id desc')->page(1, 5)->select();
        if($dynamics){
            $siteurl = C("SiteUrl");
            require_once 'ThinkPHP/Library/Vendor/autoload.php';
            $ffmpeg = new \Common\Util\FFMpeg();
            $ffmpeg = $ffmpeg->ffmpeg;
            $cover_path = './'.C('UPLOAD_PATH').'/videocover/';
            if(!is_dir($cover_path)){
                mkdir($cover_path, 0755, true);
            }
            foreach ($dynamics as $key => &$dynamic) {
                $dynamic['imgs'] = $dynamic['imgs'] ? explode(',', $dynamic['imgs']) : [];
                $video_cover = '';
                if($dynamic['video']){
                    try {
                        // 视频路径
                        $video_path = str_replace($siteurl, './', $dynamic['video']);
                        if(!is_file($video_path)){
                            $video_path = str_replace('http://www.yuzhuli.com/', './', $dynamic['video']);
                        }
                        if(is_file($video_path)){
                            $poster_path = $cover_path.md5($video_path).'.jpg';// 生成封面保存路径
                            if(!is_file($poster_path)){
                                $video = $ffmpeg->open($video_path);
                                $frame = $video->frame(\FFMpeg\Coordinate\TimeCode::fromSeconds(1));
                                $frame->save($poster_path, false);  
                            }
                            $video_cover = str_replace('/./', '/', $siteurl.$poster_path);
                        }
                    } catch (\Exception $e) {
                        trace('生成图片失败:'.$e->getMessage(), 'error');
                    }
                }
                $dynamic['video_cover'] = $video_cover;
            }
        }
        // 粉丝
        if($this->uid){
            $followHas = M('user_follow')->where(['user_id'=>$this->uid, 'follow_uid'=>$id])->find();
        }
        $info['is_follow'] = !empty($followHas) ? 1 : 0;
        // 访客
        $visitors = M('user_visitor')->alias('v')->join('__USER__ u ON v.user_id=u.id', 'left')->where(['v.visitor_uid'=>$id])->field('u.id,u.nickname,u.img,u.is_nearby')->order('v.update_time desc')->page(1, 8)->select();
        $visitor_num = M('user_visitor')->alias('v')->join('__USER__ u ON v.user_id=u.id', 'left')->where(['v.visitor_uid'=>$id])->count();
        if($this->uid){
            $visitorHas = M('user_visitor')->where(['user_id'=>$this->uid, 'visitor_uid'=>$id])->find();
            if($visitorHas){
                M('user_visitor')->where(['id'=>$visitorHas['id']])->save(['update_time'=>time(), 'num'=>$visitorHas['num']+1]);
            }else{
                M('user_visitor')->add([
                    'user_id'       => $this->uid,
                    'visitor_uid'   => $id,
                    'num'           => 1,
                    'create_time'   => time(),
                    'update_time'   => time(),
                ]);
            }
        }
        $info['visitor_num'] = $visitor_num;
        // 附近商家优惠券
        $city = $info['nearby_city'];
        // $city = '上海市';

        $shopWhere = ['s.city'=>['like', '%'.$city.'%'], 's.status'=>1];
        if($this->system){
            $shopWhere['systems'] = ['like', '%'.$this->system.'%'];
        }
        $shops = M('shop')->alias('s')
        ->join('__SHOP_COUPONS__ sc ON sc.shop_id=s.id', 'left')
        ->where($shopWhere)
        ->field('s.id,lng,lat,count(sc.id) as sc_num')
        ->group('s.id')
        ->having('sc_num > 0')
        ->select();
        // 附件商家
        $recentShopIds = [];
        if($shops){
            foreach ($shops as $key => &$shop) {
                $distance = ($shop['lng'] - $lng) * ($shop['lng'] - $lng) + ($shop['lat'] - $lat) * ($shop['lat'] - $lat);
                $shop['distance'] = round(sqrt($distance), 5);
                if($shop['distance'] <= 1000*10){// 10公里以内
                    $recentShopIds[] = $shop['id'];
                }
            }
            // $shops = $this->array_sort($shops, 'distance', 'asc');
        }
        $coupons = [];
        if($recentShopIds){
            $coupons = M('shop_coupons')->alias('sc')
            ->join('__SHOP_COUPON_TPLS__ t ON sc.tpl_id=t.id', 'left')
            ->where(['sc.shop_id'=>['in', $recentShopIds]])
            ->field('sc.id,sc.shop_id,sc.title,sc.tpl_id,sc.is_condition,sc.condition_amount,sc.discount_amount,sc.get_start_time,sc.get_end_time,sc.use_start_time,sc.use_end_time,sc.create_time,t.img')
            ->select();
            if($coupons){
                foreach ($coupons as $key => &$coupon) {
                    $coupon['get_start_time'] = date('Y-m-d', $coupon['get_start_time']);
                    $coupon['get_end_time'] = date('Y-m-d', $coupon['get_end_time']);
                    $coupon['use_start_time'] = date('Y-m-d', $coupon['use_start_time']);
                    $coupon['use_end_time'] = date('Y-m-d', $coupon['use_end_time']);
                    $coupon['create_time'] = date('Y-m-d H:i:s', $coupon['create_time']);
                    $coupon['condition_amount'] = $coupon['condition_amount']*1;
                    $coupon['discount_amount'] = $coupon['discount_amount']*1;
                }
            }
        }

        $talk_colors = ['#ff9090', '#ff92e3', '#a8dbff'];
        // 恋爱话术
        $love_talks = M('love_talk')->where(['status'=>1])->order('sort desc,id asc')->field('id,title')->select();
        foreach ($love_talks as $key => &$love_talk) {
            $i = $key%3;
            $love_talk['color'] = isset($talk_colors[$i]) ? $talk_colors[$i] : '';
        }
        // 案例
        $case_id = 112;
        // 精辟话术
        $jp_talks = M('jp_talk')->where(['status'=>1])->order('sort desc,id asc')->field('id,title')->select();
        foreach ($jp_talks as $key => &$jp_talk) {
            $i = $key%3;
            $jp_talk['color'] = isset($talk_colors[$i]) ? $talk_colors[$i] : '';
        }
        $is_buy_jp_talk = 0;
        if($this->uid){
            $jpOrder = M('jp_talk_order')->where(['user_id'=>$this->uid, 'pay_status'=>1, 'failure_time'=>['egt', time()]])->find();
            if($jpOrder){
                $is_buy_jp_talk = 1;
            }
        }
        $data = [
            'info'              => $info,// 详情
            'coupons'           => $coupons,// 优惠券
            'dynamics'          => $dynamics,
            'visitors'          => $visitors,// 访客
            'love_talks'        => $love_talks,// 恋爱话术
            'case_id'           => $case_id,// 案例id
            'jp_talks'          => $jp_talks,// 精辟话术
            'is_buy_jp_talk'    => $is_buy_jp_talk,// 是否购买了精辟话术
            'jp_talk_card'       => [
                'image' => yzl_config('jp_talk.jp_card_image'),
                'month' => yzl_config('jp_talk.jp_valid_month'),
                'money' => yzl_config('jp_talk.jp_card_money'),
            ],
        ];
        $this->ajaxReturn(array('s' => true, 'd'=>$data));
    }

    /**
     * 恋爱话术
     */
    public function lovetalk()
    {
        $id = I('id');
        $info = M('love_talk')->where(['id'=>$id, 'status'=>1])->find();
        if(empty($info)){
            $this->ajaxReturn(array('s' => false, 'm'=>'信息不存在'));
        }
        $contents = json_decode($info['contents'], true);
        $contents = $contents ? $contents : [];
        $this->ajaxReturn(array('s' => true, 'info'=>['title'=>$info['title']], 'list' => $contents));
    }

    /**
     * 精辟话术
     */
    public function jptalk()
    {
        $is_buy_jp_talk = 0;
        if($this->uid){
            $jpOrder = M('jp_talk_order')->where(['user_id'=>$this->uid, 'pay_status'=>1, 'failure_time'=>['egt', time()]])->find();
            if($jpOrder){
                $is_buy_jp_talk = 1;
            }
        }
        if(!$is_buy_jp_talk){
            $this->ajaxReturn(array('s' => false, 'm'=>'请先购买精辟话术'));
        }
        $id = I('id');
        $info = M('jp_talk')->where(['id'=>$id, 'status'=>1])->find();
        if(empty($info)){
            $this->ajaxReturn(array('s' => false, 'm'=>'信息不存在'));
        }
        $contents = json_decode($info['contents'], true);
        $contents = $contents ? $contents : [];
        $this->ajaxReturn(array('s' => true, 'info'=>['title'=>$info['title']], 'list' => $contents));
    }

    /**
     * 精辟话术购买
     */
    public function jptalk_pay()
    {
        $order = M('jp_talk_order')->where(['user_id'=>$this->uid, 'pay_status'=>1, 'failure_time'=>['egt', time()]])->find();
        if($order){
            $this->ajaxReturn(array('s' => false, 'm'=>'您已购买过'));
        }
        $valid_month = yzl_config('jp_talk.jp_valid_month');
        if(empty($valid_month)){
            $this->ajaxReturn(array('s' => false, 'm'=>'会员卡有效时间有误，无法购买'));
        }
        $pay_money = yzl_config('jp_talk.jp_card_money');
        $order_sn = 'JP'.date('YmdHis').mt_rand(10000, 99999);
        $order_id = M('jp_talk_order')->add([
            'user_id'       => $this->uid,
            'order_sn'      => $order_sn,
            'pay_money'     => $pay_money,
            'valid_month'   => $valid_month,
            'pay_status'    => 0,
            'create_time'   => time(),
            'update_time'   => time(),
        ]);
        $this->ajaxReturn(['s'=>true, 'm'=>'提交成功', 'order_sn'=>$order_sn]);
    }


    /**
     * 访客类表
     */
    public function visitors()
    {
        $id = I('id');
        $visitors = M('user_visitor')->alias('v')->join('__USER__ u ON v.user_id=u.id', 'left')->where(['v.visitor_uid'=>$id])->field('u.id,u.nickname,u.img,v.num,u.is_nearby')->order('v.update_time desc')->select();
        if($visitors){
            if($user_ids = array_column($visitors, 'id')){
                $follow_users = M('user_follow')->where(['user_id'=>$this->uid, 'follow_uid'=>['in', $user_ids]])->field('follow_uid')->select();
            }else{
                $follow_users = [];
            }
            $follow_user_ids = array_column($follow_users, 'follow_uid');
            foreach ($visitors as $key => &$visitor) {
                if(in_array($visitor['id'], $follow_user_ids)){
                    $visitor['is_follow'] = 1;
                }else{
                    $visitor['is_follow'] = 0;
                }
            }
        }
        $this->ajaxReturn(array('s' => true, 'd' => $visitors, 'num'=>count($visitors)));
    }

    /**
     * 情感咨询师
     */
    public function counselor()
    {
        $list = M('counselor')->where(['status'=>1])->order('sort desc,id asc')->select();
        if($list){
            if($counselor_ids = array_column($list, 'id')){
                $orders = M('counselor_order')->where(['user_id'=>$this->uid, 'counselor_id'=>['in', $counselor_ids], 'pay_status'=>1])->select();
            }else{
                $orders = [];
            }
            foreach ($list as $key => &$row) {
                $counselorOrders = list_search($orders, ['counselor_id'=>$row['id']]);
                $row['is_buy_voice'] = 0;
                $row['is_buy_interview'] = 0;
                if(!empty($counselorOrders)){
                    if(list_search($counselorOrders, ['type'=>1])){
                        $row['is_buy_voice'] = 1;
                    }
                    if(list_search($counselorOrders, ['type'=>2])){
                        $row['is_buy_interview'] = 1;
                    }
                }
            }
        }
        $this->ajaxReturn(array('s' => true, 'd' => ['list'=>$list]));
    }

    /**
     * 情感咨询师购买
     */
    public function counselor_pay()
    {
        $id = I('id');
        $type = I('type');// 类型 1：语音，2：面对面
        if(!in_array($type, ['1', '2'])){
            $this->ajaxReturn(array('s' => false, 'm'=>'支付类型不存在'));
        }
        $info = M('counselor')->where(['id'=>$id, 'status'=>1])->find();
        if(empty($info)){
            $this->ajaxReturn(array('s' => false, 'm'=>'信息不存在'));
        }
        $order = M('counselor_order')->where(['user_id'=>$this->uid, 'counselor_id'=>$id, 'type'=>$type, 'pay_status'=>1])->find();
        if($order){
            $this->ajaxReturn(array('s' => false, 'm'=>'您已购买过'));   
        }
        if($type == 1){
            $pay_money = $info['voice_money'];
        }else if($type == 2){
            $pay_money = $info['interview_money'];
        }
        $order_sn = 'ZX'.date('YmdHis').mt_rand(10000, 99999);
        $order_id = M('counselor_order')->add([
            'user_id'       => $this->uid,
            'order_sn'      => $order_sn,
            'counselor_id'  => $id,
            'type'          => $type,
            'pay_money'     => $pay_money,
            'pay_status'    => 0,
            'create_time'   => time(),
            'update_time'   => time(),
        ]);
        $this->ajaxReturn(['s'=>true, 'm'=>'提交成功', 'order_sn'=>$order_sn]);
    }

    /**
     * 语音视频
     */
    public function videos()
    {
        $page = I('page');
        $size = I('size');

        $list = M('nearby_videos')->where(['status'=>1])->field('id,title,cover,video')->order('sort desc,id asc')/*->page($page, $size)*/->select();
        $count = M('nearby_videos')->where(['status'=>1])->count();
        $this->ajaxReturn(array('s' => true, 'd' => $list, 'size'=>count($list), 'count'=>(int)$count));
    }

    /**
     * 关注
     */
    public function follow()
    {
        $id = I('id');
        $info = M('user')->where(['id'=>$id])->find();
        if(empty($info)){
            $this->ajaxReturn(array('s' => false, 'm'=>'信息不存在'));
        }
        $has = M('user_follow')->where(['user_id'=>$this->uid, 'follow_uid'=>$id])->find();
        if(empty($has)){
            M('user_follow')->add([
                'user_id'       => $this->uid,
                'follow_uid'    => $id,
                'create_time'   => time(),
            ]);
            M('user')->where(['id'=>$id])->setInc('follow_num');
            $this->ajaxReturn(array('s' => true, 'm' => '关注成功'));
        }else{
            $this->ajaxReturn(array('s' => true, 'm' => '关注成功'));
        }
    }

    /**
     * 取关
     */
    public function unfollow()
    {
        $id = I('id');
        $has = M('user_follow')->where(['user_id'=>$this->uid, 'follow_uid'=>$id])->find();
        if(empty($has)){
            $this->ajaxReturn(array('s' => true, 'm' => '取注成功'));
        }else{
            M('user_follow')->where(['id'=>$has['id']])->delete();
            if(M('user')->where(['id'=>$id])->getField('follow_num') >= 1){
                M('user')->where(['id'=>$id])->setDec('follow_num');
            }
            $this->ajaxReturn(array('s' => true, 'm' => '取注成功'));
        }
    }
}